const bulletConfig = require('../../scripts/configs/bulletConfig');
var Game = require('Game');

cc.Class({
    extends: cc.Component,

    properties: {
        

        bulletPrefab: {
            default: null,
            type: cc.Prefab
        },

        bulletInfoLb: {
            default: null,
            type: cc.Label
        },


        bulletLevel: 0,
        curBulletInfo: null,
    },

    getCurBulletInfo(){
        if (this.curBulletInfo == null || this.curBulletInfo.level != this.bulletLevel) {
            this.curBulletInfo = this.getBulletCurConfig();
        }
        return this.curBulletInfo;
    },

    updateBullet(){
        if(this.game == null || this.getCurBulletInfo() == null){return;}
        if (this.getCurBulletInfo().upScore <= this.game.score) {
            this.game.score -= this.getCurBulletInfo().upScore;
            this.bulletLevel++;
            this.game.drawEffect(this.node, 1, 'message1', '武器升级!!!');
            this.setBulletShowInfoLb();
        }
    },

    getBulletCurConfig() {
        if (bulletConfig.bulletLevels[this.bulletLevel]) {
            return JSON.parse(JSON.stringify(bulletConfig.bulletLevels[this.bulletLevel]));
        }
        throw '武器等级错误' + this.bulletLevel;
    },

    setBulletShowInfoLb(){
        if (this.bulletInfoLb != null) {
            this.bulletInfoLb.string = "武器等级：" + (this.bulletLevel + 1);
        }
    },

    newBullet(){
        if (this.bulletPool.size() > 0) {
            return this.bulletPool.get();
        }else{
            return cc.instantiate(this.bulletPrefab);
        }
    },

    drawBullet(){

        if(this.game.gameOver) return;

        //let bulletCount  = this.getCurBulletInfo().oneNumMin + Math.floor(Math.random() * (this.getCurBulletInfo().oneNumMax - this.getCurBulletInfo().oneNumMin));

        let bulletCountEnd = 1;// Math.min(bulletCount, this.getCurBulletInfo().FullNumMax - this.onLineEnemyCount);

        for (var i = 0; i < bulletCountEnd; i++) {

            let bullet = this.newBullet();

            if(bullet == null) continue;
            // 将新增的节点添加到 Canvas 节点下面
            this.node.addChild(bullet);

            var maxX = this.node.width / 2;

            //randX = 0;//(Math.random() - 0.5) * 2 * maxX;

            bullet.setPosition(this.game.player.x, this.game.player.y + this.game.player.height + bullet.height / 2);

            bullet.getComponent('Bullet').initInfo(this.getCurBulletInfo(), this);
        }
        
    },

    delBullet(bullet){
        this.bulletPool.put(bullet);
    },

    setGameOver(){
        // for (var i = this.onLineEnemyList.length - 1; i >= 0; i--) {
        //     this.delBullet(this.onLineEnemyList[i]);
        // }
        // this.onLineEnemyList = this.onLineEnemyList.splice(0, onLineEnemyList.length);
        this.bulletLevel = 0;
    },

    startGame(){
        // for (var i = this.onLineEnemyList.length - 1; i >= 0; i--) {
        //     this.delBullet(this.onLineEnemyList[i]);
        // }
        // this.onLineEnemyList = this.onLineEnemyList.splice(0, onLineEnemyList.length);
        this.setBulletShowInfoLb();
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.bulletPool = new cc.NodePool();
        let bullet = cc.instantiate(this.bulletPrefab);
        this.bulletPool.put(bullet);
        this.game = Game.getInstance();

        this.bulletLevel = 0;
        this.setBulletShowInfoLb();
    },

    start () {

    },

    // update (dt) {},
});

